<template>
  <div class="mypanel">
    <el-dialog :visible="showDialog" :title="form.id?'编辑权限':'新增权限'">
      <el-form :model="form" label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="企业可见">
          <el-switch
            v-model="form.enVisible"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="pancel">取 消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//导入获取权限点详情接口
import { getPerDetailApi, updatePerApi, addPerApi } from '@/api/permission'
export default {
  name: 'mypanel',
  props: ['showDialog'],
  data () {
    return {
      //表单的数据源
      form: {
        name: '', //权限名称
        code: '', //权限标识
        description: '', //权限描述
        enVisible: '' //企业可见
      }
    }
  },
  methods: {
    //用来接收  type && pid  这两个参数
    setprop (type, pid) {
      ;(this.form.type = type), (this.form.pid = pid)
    },
    //根据id得到数据源
    async getDetailList (id) {
      const res = await getPerDetailApi(id)
      // console.log(res);
      this.form = res.data
    },
    //点击弹窗的取消按钮
    pancel () {
      this.$emit('update:showDialog', false)
    },
    //点击弹窗的确定按钮
    async submit () {
      try {
        //判断当前点击是新增操作还是编辑操作
        if (this.form.id) {
          //得到修改之后的数据  //this.form
          //将编辑数据提交到服务器
          await updatePerApi(this.form)
        } else {
          //新增操作
          await addPerApi(this.form)
        }
        //提示操作成功
        this.$message.success('更新成功')
        //关闭面板
        this.$emit('update:showDialog', false)
        //清空数据
        this.form = {
          name: '', //权限名称
          code: '', //权限标识
          description: '', //权限描述
          enVisible: '' //企业可见
        }
        //更新数据
        this.$emit('updateData')
      } catch (error) {}
    }
  }
}
</script>

<style></style>
